﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;

    public class GenericButton {

        public static var UPSTATE:String = "up";
        public static var OVERSTATE:String = "over";
        public static var DOWNSTATE:String = "down";
        public static var HITSTATE:String = "hit";

        private var m_overStateScale:Number = 1.14;
        private var m_currentScaling:Number = 1;
        private var m_buttonValue:String = "";
        private var m_target:MovieClip;
        private var m_text:String;
        private var m_disabled:Boolean = false;
        private var m_callback:Function = null;
        public var m_overlay:MovieClip;
        private var m_tfOver:TextFormat;
        private var m_tfNormal:TextFormat;
        private var m_state:String = "up";
        private var m_enableHitState:Boolean = false;
        private var m_hitFrameNumber:int = 3;
        private var m_curFrame:int = 1;
        private var m_enterFrameOn:Boolean = false;
        private var normalTransform:Matrix;
        private var scaledTransform:Matrix;
        private var targetTransform:Matrix;

        public function GenericButton(_arg1:MovieClip, _arg2:Function=null, _arg3:String="", _arg4:String=""){
            var _local7:MovieClip;
            this.m_overlay = new MovieClip();
            super();
            this.m_tfOver = new TextFormat();
            this.m_tfOver.size = 15;
            this.m_tfNormal = new TextFormat();
            this.m_tfNormal.size = 14;
            this.m_target = _arg1;
            var _local5:Rectangle = this.m_target.getBounds(this.m_target.parent);
            this.m_overlay.graphics.beginFill(0xFF, 0);
            this.m_overlay.graphics.drawRect(0, 0, _local5.width, _local5.height);
            this.m_overlay.graphics.endFill();
            var _local6:MovieClip = (this.m_target.parent as MovieClip);
            if (_arg4 != ""){
                _local7 = (_local6.getChildByName(_arg4) as MovieClip);
                if (_local7 != null){
                    _local6.removeChild(_local7);
                };
            } else {
                _arg4 = "gb_Overlay";
            };
            this.m_target.parent.addChild(this.m_overlay);
            this.m_overlay.x = _local5.x;
            this.m_overlay.y = _local5.y;
            this.m_overlay.buttonMode = true;
            this.m_overlay.name = _arg4;
            this.m_overlay.buttonPointer = this;
            _arg1.buttonMode = true;
            _arg1.useHandCursor = true;
            _arg1.gotoAndStop(1);
            this.m_overlay.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            this.m_overlay.addEventListener(MouseEvent.MOUSE_OUT, this.onOut);
            this.m_overlay.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            if (_arg2 != null){
                this.m_callback = _arg2;
                this.m_overlay.addEventListener(MouseEvent.CLICK, this.onClick);
            };
            _arg1.addFrameScript(0, this.frameScriptTextUpdate);
            _arg1.addFrameScript(1, this.frameScriptTextUpdate);
            _arg1.addFrameScript(2, this.frameScriptTextUpdate);
            _arg1.addFrameScript(3, this.frameScriptTextUpdate);
            this.m_text = "";
            if (((this.m_target.generic_tf) && (this.m_target.generic_tf.text))){
                this.m_text = this.m_target.generic_tf.text;
                this.normalTransform = this.m_target.generic_tf.transform.matrix.clone();
                this.calcScaledTransform();
                this.targetTransform = this.normalTransform;
            };
            this.buttonValue = _arg3;
        }
        private function calcScaledTransform():void{
            var _local1:Number = (this.m_target.generic_tf.x + (this.m_target.generic_tf.width / 2));
            var _local2:Number = (this.m_target.generic_tf.y + (this.m_target.generic_tf.height / 2));
            this.scaledTransform = this.normalTransform.clone();
            this.scaledTransform.translate(-(_local1), -(_local2));
            this.scaledTransform.scale(this.m_overStateScale, this.m_overStateScale);
            this.scaledTransform.translate(_local1, _local2);
        }
        private function onOver(_arg1:MouseEvent):void{
            if (!this.disabled){
                this.m_target.addFrameScript(1, this.frameScriptTextUpdate);
                this.m_enterFrameOn = false;
                if (((!(this.m_enableHitState)) || (((this.m_enableHitState) && (!((this.m_state == HITSTATE))))))){
                    this.targetTransform = this.scaledTransform;
                    this.m_currentScaling = this.m_overStateScale;
                    this.m_state = OVERSTATE;
                    this.m_target.gotoAndStop(this.m_target.totalFrames);
                    this.m_target.gotoAndStop(2);
                    Global.stage.invalidate();
                    this.m_target.addEventListener(Event.RENDER, this.updateText);
                };
            };
        }
        private function onDown(_arg1:MouseEvent):void{
            this.m_target.addFrameScript(2, this.frameScriptTextUpdate);
            this.m_enterFrameOn = false;
            this.targetTransform = this.normalTransform;
            if (((!(this.m_enableHitState)) || (((this.m_enableHitState) && (!((this.m_state == HITSTATE))))))){
                this.m_state = DOWNSTATE;
                Global.stage.invalidate();
                this.m_target.addEventListener(Event.RENDER, this.updateText);
                this.m_target.gotoAndStop(this.m_target.totalFrames);
                this.m_target.gotoAndStop(3);
            };
        }
        public function onOut(_arg1:MouseEvent):void{
            this.targetTransform = this.normalTransform;
            this.m_target.addFrameScript(0, this.frameScriptTextUpdate);
            this.m_enterFrameOn = false;
            if (((!(this.m_enableHitState)) || (((this.m_enableHitState) && (!((this.m_state == HITSTATE))))))){
                this.m_state = UPSTATE;
                Global.stage.invalidate();
                this.m_target.addEventListener(Event.RENDER, this.updateText);
                this.m_target.gotoAndStop(this.m_target.totalFrames);
                this.m_target.gotoAndStop(1);
            };
        }
        private function onClick(_arg1:MouseEvent):void{
            if (!this.disabled){
                this.m_target.addFrameScript((this.m_hitFrameNumber - 1), this.frameScriptTextUpdate);
                this.m_enterFrameOn = false;
                this.targetTransform = this.normalTransform;
                this.m_state = HITSTATE;
                Global.stage.invalidate();
                this.m_target.addEventListener(Event.RENDER, this.updateText);
                this.m_target.gotoAndStop(this.m_target.totalFrames);
                this.m_target.gotoAndStop(this.m_hitFrameNumber);
                if (this.m_callback != null){
                    this.m_callback(_arg1);
                };
            };
        }
        public function set name(_arg1:String):void{
            this.m_overlay.name = _arg1;
        }
        public function get name():String{
            return (this.m_overlay.name);
        }
        public function set text(_arg1:String):void{
            this.m_curFrame = this.m_target.currentFrame;
            this.m_target.gotoAndStop(this.m_target.totalFrames);
            this.m_target.gotoAndStop(this.m_curFrame);
            if (this.m_target.generic_tf){
                this.m_target.generic_tf.text = _arg1;
                this.m_text = _arg1;
            } else {
                this.m_target.addFrameScript(this.m_curFrame, this.frameScriptTextUpdate);
            };
        }
        public function set tfOver(_arg1:TextFormat):void{
            this.m_tfOver = _arg1;
        }
        public function set tfNormal(_arg1:TextFormat):void{
            this.m_tfNormal = _arg1;
        }
        public function set state(_arg1:String):void{
            switch (_arg1){
                case UPSTATE:
                    this.m_target.gotoAndStop(1);
                    this.m_state = UPSTATE;
                    break;
                case OVERSTATE:
                    this.m_target.gotoAndStop(2);
                    this.m_state = OVERSTATE;
                    break;
                case DOWNSTATE:
                    this.m_target.gotoAndStop(3);
                    this.m_state = DOWNSTATE;
                    break;
                case HITSTATE:
                    this.m_target.gotoAndStop(this.m_hitFrameNumber);
                    this.m_state = HITSTATE;
                    break;
            };
        }
        public function get state():String{
            return (this.m_state);
        }
        public function enableHitState(_arg1:Boolean):void{
            this.m_enableHitState = _arg1;
        }
        public function setHitFrameNumber(_arg1:int):void{
            if ((((_arg1 > 0)) && ((_arg1 <= this.m_target.totalFrames)))){
                this.m_hitFrameNumber = _arg1;
            };
        }
        public function turnOff():void{
            this.m_target.buttonMode = false;
            this.m_target.useHandCursor = false;
            this.m_target.visible = false;
            this.m_overlay.buttonMode = false;
            this.m_overlay.visible = false;
            this.m_overlay.removeEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            this.m_overlay.removeEventListener(MouseEvent.MOUSE_OUT, this.onOut);
            this.m_overlay.removeEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            if (this.m_callback != null){
                this.m_overlay.removeEventListener(MouseEvent.CLICK, this.onClick);
            };
        }
        public function turnOn():void{
            this.turnOff();
            this.m_target.visible = true;
            this.m_target.buttonMode = true;
            this.m_target.useHandCursor = true;
            this.m_overlay.buttonMode = true;
            this.m_overlay.visible = true;
            this.m_overlay.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            this.m_overlay.addEventListener(MouseEvent.MOUSE_OUT, this.onOut);
            this.m_overlay.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            if (this.m_callback != null){
                this.m_overlay.addEventListener(MouseEvent.CLICK, this.onClick);
            };
        }
        public function toggleDisable(_arg1:Boolean):void{
            this.disabled = !(_arg1);
        }
        private function frameScriptTextUpdate():void{
            if (((this.m_target.generic_tf) && (!((this.m_text == ""))))){
                this.m_target.generic_tf.setTextFormat(this.m_tfOver);
                this.m_target.generic_tf.text = this.m_text;
                this.m_enterFrameOn = false;
                this.m_target.generic_tf.transform.matrix = this.targetTransform;
            } else {
                if (!this.m_enterFrameOn){
                    this.m_target.addEventListener(Event.ENTER_FRAME, this.updateText);
                    this.m_enterFrameOn = true;
                };
            };
        }
        private function updateText(_arg1:Event):void{
            if (((this.m_target.generic_tf) && (!((this.m_text == ""))))){
                this.m_target.generic_tf.setTextFormat(this.m_tfOver);
                this.m_target.generic_tf.text = this.m_text;
                this.calcScaledTransform();
                this.m_target.removeEventListener(Event.ENTER_FRAME, this.updateText);
            };
        }
        public function getButton():MovieClip{
            return (this.m_target);
        }
        public function set disabled(_arg1:Boolean):void{
            this.m_disabled = _arg1;
            if (_arg1){
                this.m_overlay.buttonMode = false;
                this.m_overlay.visible = false;
            } else {
                this.m_overlay.buttonMode = true;
                this.m_overlay.visible = true;
            };
        }
        public function get disabled():Boolean{
            return (this.m_disabled);
        }
        public function set buttonValue(_arg1:String):void{
            this.m_overlay.buttonValue = _arg1;
            this.m_buttonValue = _arg1;
        }
        public function get buttonValue():String{
            return (this.m_buttonValue);
        }

    }
}//package Display 
